<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>根据地址查询经纬度</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=LjmecQ49Sk9ywPXsiPimWEw5kNxmqvgw"></script>
</head>
<body>
    <div style="width:770px;margin:auto;">   
        要查询的地址：<input id="address" type="text" value="长沙市岳麓区云栖谷" style="margin:0 0 10px 40px; width:50%;"/>
        <button type="button" onclick="searchByStationName();">标注地址</button>
        <button type="button" onclick="window.close();">关闭</button>
        <div id="allmap" style="width: 770px; height: 460px"></div>
    </div>
</body>
<script type="text/javascript">
    var address;
        address=opener.document.getElementById("paddress").value;
        if(address !=''){document.getElementById("address").value = address;}
        var map = new BMap.Map("allmap",{minZoom:1,maxZoom:20});            // 创建Map实例
        map.centerAndZoom(new BMap.Point(112.962228, 28.22082),15);  //初始化时，即可设置中心点和地图缩放级别。
        map.centerAndZoom("长沙",15);                     // 初始化地图,设置中心点坐标和地图级别。
        map.enableScrollWheelZoom(true);//鼠标滑动轮子可以滚动
        map.enableContinuousZoom(true);    //启用地图惯性拖拽，默认禁用
        map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
        map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
        map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));   //右下角，打开
        var gc = new BMap.Geocoder();//地址解析类
        
        //拖动到位置
        var iscreatr=false;
        map.addEventListener("click", function(e){
            if(iscreatr==true)return;
            map.clearOverlays();//清空原来的标注
            //---------------------------------------------遮盖物---------------------------------------------
            iscreatr=true;
            //opener.document.getElementById("zuobiao").value = e.point.lng + ", " + e.point.lat;//打印拖动结束坐标
            console.log(e.point);
             opener.document.getElementById("lng").value = e.point.lng;//打印拖动结束坐标
             opener.document.getElementById("lat").value = e.point.lat;//打印拖动结束坐标
             var point = new BMap.Point(e.point.lng ,e.point.lat);//默认
            //获取地址信息
             gc.getLocation(e.point, function(rs){
               // console.log(rs);
               document.getElementById("address").value=rs.address;
             });
             // 创建标注对象并添加到地图  
        var marker = new BMap.Marker(point);  
             var label = new BMap.Label("可拖动修改定位信息<br />也可在搜索栏填写地址搜索，设置完成后关闭本窗口即可",{offset:new BMap.Size(20,-10)});
             marker.setLabel(label)
             map.addOverlay(marker);  
             marker.enableDragging();    //可拖拽
             marker.addEventListener("dragend", function(e){ 
                    opener.document.getElementById("lng").value = e.point.lng;//打印拖动结束坐标
                    opener.document.getElementById("lat").value = e.point.lat;//打印拖动结束坐标
             });
             marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
             //获取地址信息
             marker.addEventListener("dragend", function(e){
                    gc.getLocation(e.point, function(rs){
                    // console.log(rs);
                    document.getElementById("address").value=rs.address;
                    });
             });

        });
        //根据搜索到位置
        var localSearch = new BMap.LocalSearch(map);
            localSearch.enableAutoViewport(); //允许自动调节窗体大小
        function searchByStationName() {
            map.clearOverlays();//清空原来的标注
            var keyword = document.getElementById("address").value;
            opener.document.getElementById("paddress").value = keyword;//打印地址
            localSearch.setSearchCompleteCallback(function (searchResult) {
            var poi = searchResult.getPoi(0);
            opener.document.getElementById("lng").value = poi.point.lng;
            opener.document.getElementById("lat").value = poi.point.lat;
            map.centerAndZoom(poi.point, 13);
            var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地方对应的经纬度
            var label = new BMap.Label("可拖动修改定位信息<br />也可在搜索栏填写地址搜索，设置完成后关闭本窗口即可",{offset:new BMap.Size(20,-10)});
            marker.setLabel(label)
            map.addOverlay(marker);
            marker.enableDragging();    //可拖拽
            var content = document.getElementById("address").value + "<br/><br/>经度：" + poi.point.lng + "<br/>纬度：" + poi.point.lat;
            var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
            marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
            marker.addEventListener("dragend", function(e){ 
                    opener.document.getElementById("lng").value = e.point.lng;//打印拖动结束坐标
                    opener.document.getElementById("lat").value = e.point.lng;//打印拖动结束坐标
                    //获取地址信息
                    gc.getLocation(e.point, function(rs){
                    // console.log(rs);
                    document.getElementById("address").value=rs.address;
                    });
            });

            marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
            });
            localSearch.search(keyword);

        }
        // //根据坐标显示街道
        

</script>
</html>